package com.example.color_test.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.color_test.entity.*;
import com.example.color_test.mapper.ImgMapper;
import com.example.color_test.mapper.RecordMapper;
import com.example.color_test.service.RecordService;
import com.example.color_test.test.Calculate;
import com.example.color_test.utils.FilePath;
import com.example.color_test.utils.ResultCode;
import com.example.color_test.utils.UUIDUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;


import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Api(tags = "记录模块")
@RestController
@RequestMapping(value = "/Record")
public class RecordController {

    @Resource
    private RecordService recordService;

    @Resource
    private RecordMapper recordMapper;

    @Resource
    private ImgMapper imgMapper;

    @ApiOperation(value = "测试上传的图片覆盖率", notes = "测试上传的图片覆盖率")
    @PostMapping("/getGreenTest")
    public Object getGreenTest(@RequestBody  List<Img> imgs) {
        String keyId = UUIDUtils.getUUID();
        List<TestResult> testResults = new ArrayList<>();
        for(Img item : imgs){
            TestResult _test = Calculate.getTest(FilePath.getAllImgPath(item.getPath()));
            testResults.add(_test);
            item.setUseState(1);
            item.setKeyId(keyId);
            Img img = imgMapper.load(item.getId());
            if(img.getHistory()==1){
                item.setHistory(2);
                imgMapper.insert(item);
            }else {
                item.setHistory(1);
                imgMapper.update(item);
            }
        }
        Record record = new Record();
        record.setKeyId(keyId);
        String result= JSONArray.toJSONString(testResults);
        System.out.println(result);
        record.setTime(String.valueOf(new Date().getTime()));
        record.setResult(result);
        return recordService.insert(record);
    }

    @ApiOperation(value = "测试历史图库里的绿色覆盖率", notes = "测试历史图库里的绿色覆盖率")
    @PostMapping("/getHistoryTest")
    public Object getHistoryTest(@RequestBody  List<Img> imgs) {
        Record record = new Record();
        List<TestResult> testResults = new ArrayList<>();
        String keyId = UUIDUtils.getUUID();
        String nowTime = String.valueOf(new Date().getTime());
        for(Img item : imgs){
            TestResult _test = Calculate.getTest(FilePath.getAllImgPath(item.getPath()));
            testResults.add(_test);
            Img img = imgMapper.load(item.getId());
            if(img.getHistory()==1){
                img.setPath(item.getPath());
                img.setTime(nowTime);
                img.setKeyId(keyId);
                img.setHistory(2);
                img.setUseState(1);
                imgMapper.insert(img);
            }else {
                img.setPath(item.getPath());
                img.setTime(nowTime);
                img.setKeyId(keyId);
                img.setHistory(1);
                img.setUseState(1);
                imgMapper.update(img);
            }
        }
        String result= JSONArray.toJSONString(testResults);
        System.out.println(result);
        record.setTime(nowTime);
        record.setResult(result);
        record.setKeyId(keyId);
        return recordService.insert(record);

    }

    @ApiOperation(value = "删除记录", notes = "删除记录")
    @DeleteMapping("/delete")
    public Object delete(int id) {
        return recordService.delete(id);
    }


    @ApiOperation(value = "更新记录", notes = "更新记录")
    @PutMapping("/update")
    public Object update(Record record) {
        return recordService.update(record);
    }

    @ApiOperation(value = "查询单个记录内容", notes = "查询单个记录内容")
    @GetMapping("/load/{id}")
    public Object load(@PathVariable(required = true,value = "id") int id) {
        Record record = recordService.load(id);
        RecordResult recordResult = new RecordResult(record);
        List<Img> imgs = imgMapper.selectAll(record.getKeyId());
        recordResult.setImages(imgs);
        return ResultCode.SUCCSSES.Result("查询成功",recordResult);
    }

    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/getRecordList")
    public Object getRecordList(@RequestParam(required = true,value = "page") int page,
                                   @RequestParam(required = true,value = "size") int size,
                                   @RequestParam(required = true,value = "firstId") int firstId) {
        System.out.println(page);
        System.out.println(size);
        Page result = new Page();
        PageHelper.startPage(page, size);
        List<Record> records = new ArrayList<>();
        if (firstId > 0) {
            records = recordMapper.selectRecordsByLastId(firstId);
        } else {
            records = recordMapper.selectRecords();
        }
        PageInfo<Record> pageInfo = new PageInfo<Record>(records);
        result.setPage(pageInfo.getPageNum());
        result.setSize(pageInfo.getPageSize());
        result.setPages(pageInfo.getPages());
        result.setTotals(pageInfo.getTotal());
        List<RecordResult> recordResults = new ArrayList<>();
        for (Record item : records){
            RecordResult recordResult = new RecordResult(item);
            List<Img> imgs = imgMapper.selectAll(item.getKeyId());
            recordResult.setImages(imgs);
            recordResults.add(recordResult);
        }

        result.setDataList(recordResults);
        return ResultCode.SUCCSSES.Result("查询成功", result);
    }

}
